import { Entity } from "../../../../../Core/Module/Entity/Entity";
import { MongoHelper } from "../../../../../Core/Serialize/MongoHelper";
import { ObjectSystemAttribute, FriendOf, ExpandOf } from "../../../../../Decorator/Decorator";
import { AwakeSystem } from "../../../../../Core/Module/EventSystem/IAwakeSystem";
import { DestroySystem } from "../../../../../Core/Module/EventSystem/IDestroySystem";
import { UnitManagerComponent } from "../../../../Model/Share/Module/Unit/UnitManagerComponent";


/**
 * 使用 Expand 扩展方法
 * 
 * 必须作出以下申明，才能调用对象的扩展方法
 */
declare module "../../../../Model/Share/Module/Unit/UnitManagerComponent" {

    interface UnitManagerComponent {
        GetEntity<T extends Entity>(clazz: typeof Entity, data: any): T;
    }

}

@ExpandOf(UnitManagerComponent)
export class UnitManagerComponentSystem {

    private static GetEntity<T extends Entity>(self: UnitManagerComponent, clazz: typeof Entity, data: any): T {
        let entity = self.AddChild<T>(clazz);
        self.RemoveChild(entity.Id);
        MongoHelper.FromBson(entity, data);
        return entity;
    }

}

@FriendOf(UnitManagerComponent)
@ObjectSystemAttribute()
export class UnitManagerComponentAwakeSystem extends AwakeSystem<UnitManagerComponent> {

    Awake(self: UnitManagerComponent): void {
        UnitManagerComponent.Instance = self;
    }

}

@FriendOf(UnitManagerComponent)
@ObjectSystemAttribute()
export class UnitManagerComponentDestroySystem extends DestroySystem<UnitManagerComponent> {

    Destroy(self: UnitManagerComponent): void {
        UnitManagerComponent.Instance = null;
    }

}